package com.ruoyi.service;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.KongAdmin;
import com.ruoyi.utils.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;

@Service("ThrottlingKong")
public class ThrottlingKong {
    @Autowired
    KongAdmin kongAdmin;


    public JSONObject throttlingInService(JSONObject requestBody, String serviceName){
     String response =  kongAdmin.CreatePluginInService(requestBody,serviceName,"plugins");
        JSONObject jsonObject = JSONObject.parseObject(response);
        if(Check.CheckIsSuccess(jsonObject)){
                jsonObject.put("KongCode",200);
        }else{
            jsonObject.put("KongCode",500);
        }
        return jsonObject;
    }
 public JSONObject throttlingInConsumer(JSONObject requestBody, String cname){
        String response =  kongAdmin.CreatePluginInConsumer(requestBody,cname,"plugins");
        JSONObject jsonObject = JSONObject.parseObject(response);
        if(Check.CheckIsSuccess(jsonObject)){
            jsonObject.put("KongCode",200);
        }else{
            jsonObject.put("KongCode",500);
        }
        return jsonObject;
 }

 public JSONObject updatethrottling(JSONObject requestBody, String pluginsID){

       String response = kongAdmin.PatchPlugin(pluginsID,requestBody);
   JSONObject jsonObject = JSONObject.parseObject(response);
    if(Check.CheckIsSuccess(jsonObject)){
       jsonObject.put("KongCode",200);
    }else{
       jsonObject.put("KongCode",500);
   }
        return jsonObject;
 }


 public int deleteThrottling(String pluginsID){
     String response="";

            response=  kongAdmin.DeletePluginByID(pluginsID);

        return 1 ;
 }
}
